﻿using System;
using System.Data.Linq;
using System.Linq;
using WebUI.DAL;
using System.Collections.Generic;

namespace WebUI.BL.Email
{
    public class EmailBL
    {
        public void clsEmailInsert(tbl_Email emails)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    tbl_Email email = new tbl_Email();
                    email.Email = emails.Email;
                    email.Customer = emails.Customer;
                    email.RegisterDate = emails.RegisterDate;
                    email.UserNameLogin = emails.UserNameLogin;
                    email.Status = emails.Status;

                    adlerContextDb.tbl_Emails.InsertOnSubmit(email);
                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsEmailModify(tbl_Email emails)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var email = adlerContextDb.tbl_Emails.Single(d => d.EmailNo == emails.EmailNo);
                    email.Email = emails.Email;
                    email.Customer = emails.Customer;
                    email.RegisterDate = emails.RegisterDate;
                    email.UserNameLogin = emails.UserNameLogin;
                    email.Status = emails.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        protected void clsEmailDelete(tbl_Email emails)
        {
            using (AdlerContextDataContext adlerContextDb = new AdlerContextDataContext())
            {
                try
                {
                    var email = adlerContextDb.tbl_Emails.Single(d => d.EmailNo == emails.EmailNo);
                    email.Status = emails.Status;

                    adlerContextDb.SubmitChanges();
                }
                catch (ChangeConflictException)
                {
                    //neu xay ra loi rollback lai
                    adlerContextDb.ChangeConflicts.ResolveAll(RefreshMode.KeepCurrentValues);
                    adlerContextDb.SubmitChanges();
                }
            }
        }

        // Show Email.
        public List<MODEL_SP_ADMIN_SEARCH_EMAIL> Get_Admin_Emails(string email, string datefrom, string dateto, string status)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                int index = 1;
                //var query = from n in contextdb.SP_ADMIN_SEARCH_EMAIL(email, datefrom, dateto, status)
                //            select new MODEL_SP_ADMIN_SEARCH_EMAIL
                //            {
                //                Index = index++,
                //                EmailNo = n.EmailNo,
                //                Email = n.Email,
                //                Customer = n.Customer,
                //                RegisterDate = n.RegisterDate,
                //                UserNameLogin = n.UserNameLogin,
                //                Status = n.Status
                //            };
                //return query.ToList();
                return null;
            }
        }

        public tbl_Email GetEmails(int Emailsno)
        {
            using (AdlerContextDataContext contextdb = new AdlerContextDataContext())
            {
                return contextdb.tbl_Emails.Single(d => d.EmailNo == Emailsno);
            }
        }
    }
}
